package com.miaoxun.service.impl;


import com.basemapper.BasicMapper;
import com.miaoxun.dao.UserMapper;
import com.miaoxun.dao.UserRoleMapper;
import com.miaoxun.domain.entity.User;
import com.miaoxun.domain.entity.UserRole;
import com.miaoxun.domain.model.UserModel;
import com.miaoxun.domain.param.UserParam;
import com.miaoxun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    protected BasicMapper getMapper() {
        return userMapper;
    }


    @Override
    public List<User> listByDept(UserParam param) {
        return userMapper.listByDept(param);
    }

    @Override
    public User findByUsername (String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public boolean resetPwd(User user) {
        return update(user);
    }

    @Override
    public UserModel findUserDeptById(Long id) {
        return userMapper.findUserDeptById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateUserAndRole(UserParam param) {

        User user = new User();
        user.setUserId(param.getUserId());
        user.setStatus(param.getStatus());
        user.setDeptId(param.getDeptId());
        user.setEmail(param.getEmail());
        user.setGmtModified(new Date());
        user.setMobile(param.getMobile());
        user.setName(param.getName());
        user.setUsername(param.getUsername());
        boolean r = update(user);
        Long userId = user.getUserId();
        List<Long> roles = param.getRoleIds();
        userRoleMapper.removeByUserId(userId);
        List<UserRole> list = new ArrayList();
        for (Long roleId : roles) {
            UserRole ur = new UserRole();
            ur.setUserId(userId);
            ur.setRoleId(roleId);
            list.add(ur);
        }
        if (list.size() > 0) {
            userRoleMapper.batchSave(list);
        }

        return r;

    }

    @Override
    public boolean exit(String params) {
        return userMapper.findByUsername(params) != null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addUserAndRole(UserParam param) {
        User user = new User();
        user.setPassword(param.getPassword());
        user.setStatus(param.getStatus());
        user.setDeptId(param.getDeptId());
        user.setEmail(param.getEmail());
        user.setGmtModified(new Date());
        user.setMobile(param.getMobile());
        user.setName(param.getName());
        user.setUsername(param.getUsername());
        user.setUserIdCreate(param.getUserIdCreate());
        user.setGmtCreate(new Date());
        boolean count = save(user) != null;
        List<Long> roles = param.getRoleIds();
        userRoleMapper.removeByUserId(Long.parseLong(user.getBackId()));
        List<UserRole> list = new ArrayList();
        for (Long roleId : roles) {
            UserRole ur = new UserRole();
            ur.setUserId(Long.parseLong(user.getBackId()));
            ur.setRoleId(roleId);
            list.add(ur);
        }
        if (list.size() > 0) {
            userRoleMapper.batchSave(list);
        }

        return count;

    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteUserAndRoleById(Long id) {
        boolean r = deleteById(id);
        userRoleMapper.deleteByUserId(id);
        return r;
    }

}
